package com.zego.wawaji.base.utils;

import com.noble.library.basic.sp.PSP;
import com.noble.library.time.FormatPattern;
import com.noble.library.time.TimeUtil;
import com.zego.wawaji.base.module.ModuleMgr;

/**
 * 时间间隔判断
 */
public class IntervalUtil {
    /**
     * 判断是否到达第二天并进行时间存储: 微信限制登录使用
     *
     * @return true 达到第二天
     */

    private static final String CHECK_OPENID_KEY = "check_openid_key";

    private static String getCheckOpenidKey() {
        return CHECK_OPENID_KEY;
    }

    public static boolean checkOpenidDate() {
        String recommendDate = PSP.getInstance().getString(getCheckOpenidKey(), "-1");
        return !recommendDate.equals(TimeUtil.formatTime(ModuleMgr.getAppMgr().getTime(),
                FormatPattern.FP_YMD2));
    }

    public static void updateCheckOpenidDate() {
        PSP.getInstance().put(getCheckOpenidKey(), TimeUtil.formatTime(ModuleMgr.getAppMgr().getTime(),
                FormatPattern.FP_YMD2) + "");
    }

    // ================================ 重新登录限制 ===========================
    /**
     * 判断是否后台运行超过指定时间： 重新登录使用
     */
    private static final String CHECK_RELOGIN_KEY = "check_relogin_key";

    private static String getCheckReLoginKey() {
        return CHECK_RELOGIN_KEY;
    }

    public static boolean needReLogin() {
        long currentTime = ModuleMgr.getAppMgr().getTime();
        long time = PSP.getInstance().getLong(getCheckReLoginKey(), currentTime);
        long distance = (currentTime - time) / 1000;

        // 超过半小时
        return distance > 60 * 30;
    }

    public static void updateReLoginDate() {
        PSP.getInstance().put(getCheckReLoginKey(), ModuleMgr.getAppMgr().getTime());
    }

    // ================================ 广告弹框限制 ===========================
    private static final String CHECK_AD_KEY = "check_ad_key";

    /**
     * 判断是否到达第二天并进行时间存储： 广告弹框用
     *
     * @return true 达到第二天
     */
    private static String getCheckAdKey() {
        return CHECK_AD_KEY + ModuleMgr.getCenterMgr().getMyInfo().getUid();
    }

    public static boolean checkAdDate() {
        long currentTime = ModuleMgr.getAppMgr().getTime();
        long time = PSP.getInstance().getLong(getCheckAdKey(), currentTime);
        long distance = (currentTime - time) / 1000;

        // 超过半天
        return distance > 60 * 60 * 6;
    }

    /**
     * 更新第二天状态
     */
    public static void updateAdDate() {
        PSP.getInstance().put(getCheckAdKey(), ModuleMgr.getAppMgr().getTime());
    }

    // =======================  应用市场评论弹框 ========================
    private static final String MARK_INTERVAL = "mark_interval";

    private static String getMarkKey() {
        return MARK_INTERVAL + ModuleMgr.getCenterMgr().getMyInfo().getUid();
    }

    public static boolean checkMarkDate() {
        long oldTime = PSP.getInstance().getLong(getMarkKey(), 0);
        long distance = TimeUtil.getIntervalDays(oldTime, ModuleMgr.getAppMgr().getTime());
        return distance > 7;
    }

    public static void updateMarkDate() {
        PSP.getInstance().put(getMarkKey(), ModuleMgr.getAppMgr().getTime());
    }
}
